#!/usr/local/bin/python3
# -*- coding: utf-8 -*-

"""
@File    : tcp_utils.py
@Author  : Link
@Time    : 2022/12/13 21:55
@Mark    : 
"""
from PySide2.QtNetwork import QNetworkInterface, QAbstractSocket, QHostAddress


def get_local_ip():
    """获取本地主机IP"""
    for addr in QNetworkInterface.allAddresses():
        # 只要IPv4地址，过滤掉127.0.0.1
        if addr.protocol() == QAbstractSocket.NetworkLayerProtocol.IPv4Protocol \
                and addr != QHostAddress.SpecialAddress.LocalHost:
            address = addr.toString()
            # 169开头的IP地址表示本地主机未从DHCP分配到有效IP，过滤网关地址x.x.x.1
            if address[:3] != '169' and address.split('.')[-1] != '1':
                return address
    return '0.0.0.0'
